//
//  HRDynamicInfoCommentView.m
//  Hear
//
//  Created by mac on 2017/10/10.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRDynamicInfoCommentView.h"
#import "YYLabel.h"
#import "HRDynamicCommentModel.h"
static CGFloat const commentHeight = 20;
static CGFloat const leftPading = 15;
@interface HRDynamicInfoCommentView ()
@property (nonatomic, strong)YYLabel *moreLabel;
@property (nonatomic, strong)NSMutableArray *commentViews;
@property (nonatomic, strong)YYTextLayout *commentLayout;
@property (nonatomic, strong)NSArray *commentArray;
@end
@implementation HRDynamicInfoCommentView

-(void)setCommentModel:(HRDynamicCommentModel *)commentModel{
    self.commentArray = commentModel.ChildComments;
    
    
    
    for (int i = 0; i < 3; i ++) {
        YYLabel *commentLabel = self.commentViews[i];
        if (i >= self.commentArray.count){
            commentLabel.hidden = YES;
        }else{
            
            HRDynamicCommentModel *model = self.commentArray[self.commentArray.count-i-1];
            
            commentLabel.hidden = NO;
            
            
            
//            if ([model.CommentType intValue] == 2){
//                commentStr = [NSString stringWithFormat:@"%@：[语音]",model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName];
//            }else{
//                commentStr = [NSString stringWithFormat:@"%@：%@",model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName,model.CommentText];
//            }
//
//            if (model.ReplyUserLocalId.length){
//                if ([model.CommentType intValue] == 2){
//                    commentStr = [NSString stringWithFormat:@"%@[语音]",model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName];
//                }else{
//                    commentStr = [NSString stringWithFormat:@"%@：%@",model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName,model.CommentText];
//                }
//
//            }
            NSString *commentStr = @"";
            if ([model.CommentLevel intValue] ==3){
                if ([model.CommentType intValue] == 2){
                    commentStr = [NSString stringWithFormat:@"%@回复%@：[语音]",model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName,model.ReplyUserNickName];
                }else{
                    commentStr = [NSString stringWithFormat:@"%@回复%@：%@",model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName,model.ReplyUserNickName,model.CommentText];
                }
                
            }else{
                if ([model.CommentType intValue] == 2){
                    commentStr = [NSString stringWithFormat:@"%@：[语音]",model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName];
                }else{
                    commentStr = [NSString stringWithFormat:@"%@：%@",model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName,model.CommentText];
                }
            }
            
            if (commentStr.length > 30){
                commentStr = [commentStr substringToIndex:30];
            }
            
            

            NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:commentStr];
            
            [att setColor:SecondTextColor range:NSMakeRange(0, commentStr.length)];

            
            if ([model.CommentLevel intValue] ==3){
                [att setColor:FirstTextColor range:NSMakeRange(0, [model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName length])];
                [att setColor:FirstTextColor range:NSMakeRange([model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName length]+2, model.ReplyUserNickName.length)];
            }else{
                [att setColor:FirstTextColor range:NSMakeRange(0, [model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName length])];
            }
            
//
//            [att setColor:CustomColor(160, 160, 160, 1) range:NSMakeRange(0, commentStr.length)];
//
//            if (model.ReplyUserLocalId.length){
//
//
//                if ([model.CommentType intValue] == 2){
//                    [att setColor:FirstTextColor range:NSMakeRange(0, (model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName).length)];
            
//                    [att setColor:FirstTextColor range:NSMakeRange((model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName).length+2, (model.UserRemark.length>0?model.UserRemark:model.ReplyUserNickName).length)];
//                }else{
//                    [att setColor:FirstTextColor range:NSMakeRange(0, (model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName).length)];
                    
                    
//                    [att setColor:FirstTextColor range:NSMakeRange((model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName).length+2, (model.UserRemark.length>0?model.UserRemark:model.ReplyUserNickName).length)];
//                }
//
//            }else{
//                [att setColor:FirstTextColor range:NSMakeRange(0, (model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName).length)];
//            }
//
//
//            [att setFont:CustomUIFont(13) range:NSMakeRange(0, commentStr.length)];
            
            
            CGFloat commentWidth = [att boundingRectWithSize:CGSizeMake(self.width-leftPading*2, commentHeight) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size.width;
            commentLabel.frame = CGRectMake(leftPading, commentHeight*i+leftPading,commentWidth, commentHeight);
            
            
            
            YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(commentLabel.width, 20)];
            //            container.maximumNumberOfRows = 1;
            _commentLayout = [YYTextLayout layoutWithContainer:container text:att];
            commentLabel.textLayout = _commentLayout;
            
        }
    }
    
    if (self.commentArray.count > 3){
        self.moreLabel.hidden = NO;
        self.moreLabel.frame = CGRectMake(leftPading, commentHeight*3+leftPading, 100, leftPading);
        self.moreLabel.text = [NSString stringWithFormat:@"查看全部%lu条",(unsigned long)self.commentArray.count];
    }else{
        self.moreLabel.hidden = YES;
    }
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // CGRectMake(leftPading, commentHeight*i, kFullWidth-leftPading*2, commentHeight)
        
        self.commentViews = [NSMutableArray array];
        
        for (int i = 0; i < 3; i ++) {
            YYLabel *label = [YYLabel new];
            label.textColor = CustomColor(160, 160, 160, 1);
            label.font = CustomUIFont(13);
            label.tag = i;
            label.displaysAsynchronously = YES;
            label.ignoreCommonProperties = YES;
            label.fadeOnAsynchronouslyDisplay = NO;
            label.fadeOnHighlight = NO;
            [self addSubview:label];
            [self.commentViews addObject:label];
        }
        
        YYLabel *moreLabel = [YYLabel new];
        moreLabel.text = @"查看全部";
        moreLabel.font = CustomUIFont(12);
        moreLabel.textColor = CustomColor(160, 160, 160, 1);
        [self addSubview:(self.moreLabel = moreLabel)];
        
        
    }
    return self;
}

+(CGSize)sizeDynamicInfoCommentViewCount:(NSInteger)commentCount{
    CGSize commentSize = CGSizeZero;
    
    if (commentCount<4){
        commentSize = CGSizeMake(kFullWidth-leftPading*2, commentHeight*commentCount+leftPading*2);
    }else if (commentCount > 3){
        commentSize = CGSizeMake(kFullWidth-leftPading*2, commentHeight*3 +leftPading+leftPading*2);
    }
    
    return commentSize;
}

@end
